package com.chinaoly.baotou.service.impl;

import com.chinaoly.baotou.common.utils.RedisUtil;
import com.chinaoly.baotou.dao.mapper.DevMapper;
import com.chinaoly.baotou.model.TBDev;
import com.chinaoly.baotou.service.DevService;
import com.chinaoly.frm.common.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;


/**
 * 实体类服务接口实现类
 *
 * @author xuyh
 * @date 2019-11-05 17:45:36
 */
@Service
@Transactional
@Slf4j
public class DevServiceImpl implements DevService {

    @Resource
    private DevMapper devMapper;
    @Resource
    private RedisUtil redisUtil;

    @Override
    public TBDev selectByDevNumber(String deviceNumber) {
        TBDev devInfo = (TBDev) redisUtil.get("DYNAMIC:" + deviceNumber);
        if (CommonUtil.isNull(devInfo)) {
            devInfo = devMapper.selectByDevNumber(deviceNumber);
            if (!CommonUtil.isNull(devInfo)) {
                redisUtil.set("DYNAMIC:" + deviceNumber, devInfo, 1L, TimeUnit.HOURS);
            } else {
                log.info("设备编码：{}，没有检索到有效的设备信息", deviceNumber);
                return null;
            }
        }
        return devInfo;
    }
}